package com.zy.fcs.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zy.fcs.bean.LoginInfo;
import com.zy.fcs.bean.param.AfterUploadParam;
import com.zy.fcs.repository.TblMediaManagementRepository;
import com.zy.fcs.repository.TblPcmDoccdeRepository;
import com.zy.fcs.repository.TblPcmDoctypRepository;
import com.zy.fcs.service.MediaManagementService;
import com.zy.fcs.utils.ZyUtils;

/**
 * 文件上传，删除等文件管理Controller
 *
 * @author zytc-ypp
 * @date 2018/3/7
 */
@Controller
@RequestMapping("/mgr")
public class MgrController extends BaseController {

    @Autowired
    private TblPcmDoctypRepository tblPcmDoctypRepository;

    @Autowired
    private TblPcmDoccdeRepository tblPcmDoccdeRepository;

    @Autowired
    private TblMediaManagementRepository tblMediaManagementRepository;

    @Autowired
    private MediaManagementService mediaManagementService;

    private static final Logger logger = LoggerFactory.getLogger(MgrController.class);

    /**
     * 文件上传管理
     *
     * @param params 参数
     * @param model model
     * @param session session
     */
    @PostMapping(value = "/getMediaMgr")
    public String getMediaMgr(@RequestParam Map<String, Object> params, Model model, HttpSession session) {
        model.addAttribute("mediaType", "mgr");
        model.addAttribute("currentUploadDirDesc", params.get("currentUploadDirDesc"));
        model.addAttribute("currentUploadDir", params.get("currentUploadDir"));

        return this.render("mediaBody");
    }

    /**
     * 记录文件上传信息
     *
     * @param params 参数
     * @param model model
     * @param session session
     */
    @PostMapping(value = "/addMediaRecord")
    @ResponseBody
    public JSONObject addMediaRecord(@RequestParam Map<String, Object> params, Model model, HttpSession session) {

        try {
            LoginInfo loginInfo = (LoginInfo)session.getAttribute("loginInfo");
            AfterUploadParam param = new AfterUploadParam();
            param.setInstNo(loginInfo.getInstNo());
            param.setOrderNo(ZyUtils.filterNull(params.get("currentApplNo")));

            String[] strs = ZyUtils.filterNull(params.get("currentUploadDir")).split("/");
            if (strs.length > 3) {
                param.setCategory1(strs[2]);
                param.setCategory2(strs[3]);
            } else {
                throw new Exception("上传路径错误");
            }
            param.setMediaUrl(ZyUtils.filterNull(params.get("currentUploadDir")));
            param.setCreateId(loginInfo.getUserName());

            mediaManagementService.afterUpload(param);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return failedJson();
        }
        return successJson();
    }

    /**
     * 删除影像文件（只删除数据，不删除oss文件）
     *
     * @param params 影像URL
     */
    @PostMapping(value = "/deleteMedia")
    @ResponseBody
    public JSONObject deleteMedia(@RequestParam Map<String, Object> params) {

        try {
            Long id = Long.valueOf((String)params.get("id"));
            mediaManagementService.deleteMedia(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return failedJson();
        }
        return successJson();
    }
}
